/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package detonadores;

import Controladores.ControladorInsertar;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Augusto Andrade
 */
@WebServlet(name = "ProcesarPlan", urlPatterns = {"/ProcesarPlan"})
public class ProcesarPlan extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            ControladorInsertar ControlPlan=new ControladorInsertar();
            String patronNumerico = "([0-9]+)";
            String DatoPlan=request.getParameter("DatoPlan");
            String InsertarPl=null;           
            Pattern p2 = Pattern.compile(patronNumerico);
            Matcher matcherNumero = p2.matcher(DatoPlan);
            
            if(DatoPlan.equals("Ingresar")){
            out.println("<script type='text/javascript'>"
            +"alert('No se ingreso ningun Plan');"
            +"pagina='FormularioPlan.jsp';"
            +"function redireccionar(){location.href=pagina;}" 
            +"setTimeout ('redireccionar()', 0);"
            +"</script>");
            }else if(matcherNumero.matches()){
            
        InsertarPl="0,'"+DatoPlan+"'";
        ControlPlan.setContenidoPlan(InsertarPl);
        ControlPlan.IngresarPlan();
        if(ControlPlan.getContenidoPlan()==true){
        out.println("<script type='text/javascript'>"
        +"alert('El Plan se ingresó Correctamente');"
        +"pagina='FormularioPlan.jsp';"
        +"function redireccionar(){location.href=pagina;}" 
        +"setTimeout ('redireccionar()', 0);"
        +"</script>");
        }else{
        out.println("<script type='text/javascript'>"
        +"alert('El Plan NO se ingresó Correctamente vuelva a intentarlo');"
        +"pagina='FormularioPlan.jsp';"
        +"function redireccionar(){location.href=pagina;}" 
        +"setTimeout ('redireccionar()', 0);"
        +"</script>");
        }
            }else{
            out.println("<script type='text/javascript'>"
            +"alert('El Plan ("+DatoPlan+") no es valido. Solo debe ingresar (Numeros)');"
            +"pagina='FormularioPlan.jsp';"
            +"function redireccionar(){location.href=pagina;}" 
            +"setTimeout ('redireccionar()', 0);"
            +"</script>");
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
